<?php

/* * *************************************************************************** */
/*                                                                            */
/*                       __        ____                                       */
/*                 ___  / /  ___  / __/__  __ _____________ ___               */
/*                / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-<               */
/*               / .__/_//_/ .__/___/\___/\_,_/_/  \__/\__/___/               */
/*              /_/       /_/                                                 */
/*                                                                            */
/*                                                                            */
/* * *************************************************************************** */
/*                                                                            */
/* Titre          : Récupérer les informations d'une vidéos Youtube,...       */
/*                                                                            */
/* URL            : http://www.phpsources.org/scripts607-PHP.htm              */
/* Auteur         : mercier133                                                */
/* Date édition   : 12 Oct 2010                                               */
/* Website auteur : http://www.servicesgratis.net                             */
/*                                                                            */
/* * *************************************************************************** */

function getVideoInfo($url) {
    $type = "";
    $id = -1;
    $titre = "no title";
    $description = "no description";
    $code = "no code";
    $img = "no image";
    //Détermination du "type" de vidéo : 
    if (preg_match("/youtube/", $url))
        $type = "youtube";
    else if (preg_match("/dailymotion/", $url))
        $type = "dailymotion";
    else if (preg_match("/google/", $url))
        $type = "google";
    else if (preg_match("/vimeo/", $url))
        $type = "vimeo";
    else
        return false;

    //Détermination de l'"ID" de la vidéo :
    if ($type == "youtube") {
        $debut_id = explode("v=", $url, 2);
        $id_et_fin_url = explode("&", $debut_id[1], 2);
        $id = $id_et_fin_url[0];
    } else if ($type == "dailymotion") {
        $debut_id = explode("/video/", $url, 2);
        $id_et_fin_url = explode("_", $debut_id[1], 2);
        $id = $id_et_fin_url[0];
    } else if ($type == "google") {
        $debut_id = explode("docid=", $url, 2);
        $id_et_fin_url = explode("&", $debut_id[1], 2);
        $id = $id_et_fin_url[0];
    } else if ($type == "vimeo") {
        $l_id = eregi("([0-9]+)$", $url, $lid);
        $id = $lid[0];
    }

    //Analyse et stockage des informations de la vidéo
    if ($type == "youtube") {
        $xml = @file_get_contents("http://gdata.youtube.com/feeds/api/videos/" .
                        $id);
        //titre
        preg_match('#<title(.*?)>(.*)<\/title>#is', $xml, $resultTitre);
        $titre = $resultTitre[count($resultTitre) - 1];
        //description
        preg_match('#<content(.*?)>(.*)<\/content>#is', $xml, $resultDescription);
        if ($resultDescription == null) {
            $description = 'pas de description';
        } else {
            $description = $resultDescription[count($resultDescription) - 1];
        }
        //Image
        $img = "http://img.youtube.com/vi/" . $id . "/mqdefault.jpg";
        //Code HTML
        $code =
                '<object width="425" height="355"><param name="movie"' .
                ' value="http://www.youtube.com/v/' . $id .
                '&hl=fr"></param><param name="wmode" value="transparent"></param><embed' .
                ' src="http://www.youtube.com/v/' . $id .
                '&hl=fr" type="application/x-shockwave-flash" wmode="transparent" width="425"' .
                ' height="355"></embed></object>';
    } else if ($type == "dailymotion") {
        $tags = get_meta_tags("http://www.dailymotion.com/video/" . $id);
        //titre
//        $titre = htmlspecialchars(trim(str_replace("Dailymotion -", "", $tags[
//                                "title"])));
        $idString = (string) $id;
        $nbUrlDaylimotion = strlen($idString) + strlen("http://www.dailymotion.com/video/");
        $tempTitle = substr($url, $nbUrlDaylimotion+1);
        $titre = str_replace("-"," ",$tempTitle);
        //description
        $description = $tags["description"];
        //image 
        $img = "http://www.dailymotion.com/thumbnail/160x120/video/" . $id;
        // code HTML
        $code =
                '<div><object width="420" height="357"><param name="movie"' .
                ' value="http://www.dailymotion.com/swf/' . $id .
                '&v3=1&related=1"></param><param name="allowFullScreen"' .
                ' value="true"></param><param name="allowScriptAccess" value="always"></param>' .
                '<embed src="http://www.dailymotion.com/swf/' . $id .
                '&v3=1&related=1" type="application/x-shockwave-flash" width="420"' .
                ' height="357" allowFullScreen="true" allowScriptAccess="always"></embed></obj' .
                'ect></div>';
    } else if ($type == "google") {
        $xml_string = @file_get_contents(
                        "http://video.google.com/videofeed?docid=" . $id);
        //titre
        $xml_title_debut = explode("<title>", $xml_string, 2);
        $xml_title_fin = explode("</title>", $xml_title_debut[1], 2);
        $titre = $xml_title_fin[0];
        //description
        $xml_description_debut = explode("<description>", $xml_string, 2);
        $xml_description_fin = explode("</description>", $xml_description_debut[1
                ], 2);
        $description = $xml_description_fin[0];
        //image
        $xml_image_debut = explode('&lt;img src="', $xml_string, 2);
        $xml_image_fin = explode('" width="', $xml_image_debut[1], 2);
        $img = $xml_image_fin[0];
        //code HTML 
        $code =
                '<embed style="width:400px; height:326px;" id="VideoPlayback"' .
                ' type="application/x-shockwave-flash" src="http://video.google.com/googleplay' .
                'er.swf?docId=' . $id . '&hl=fr" flashvars=""> </embed>';
    } else if ($type == "vimeo") {
        $xml_string = @file_get_contents("http://vimeo.com/api/clip/" . $id . ".xml"
        );
        //titre
        $xml_title_debut = explode("<title>", $xml_string, 2);
        $xml_title_fin = explode("</title>", $xml_title_debut[1], 2);
        $titre = $xml_title_fin[0];
        //description
        $xml_description_debut = explode("<caption>", $xml_string, 2);
        $xml_description_fin = explode("</caption>", $xml_description_debut[1], 2)
        ;
        $description = $xml_description_fin[0];
        //image
        $xml_image_debut = explode("<thumbnail_large>", $xml_string, 2);
        $xml_image_fin = explode("</thumbnail_large>", $xml_image_debut[1], 2);
        $img = $xml_image_fin[0];
        //code HTML
        $xml_code = @file_get_contents(
                        "http://vimeo.com/api/oembed.xml?url=http%3A//vimeo.com/" . $id);
        $xml_code_debut = explode("<html>", $xml_code, 2);
        $xml_code_fin = explode("</html>", $xml_code_debut[1], 2);
        $code = str_replace("<![CDATA[", "", str_replace("]]>", "", $xml_code_fin[0]
        ));
    }

    return array("id" => $id, "type" => $type, "titre" => $titre, "description" =>
        $description, "img" => $img, "code" => $code);
}

?>
